package com.mingqijia.gassafety.console.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.gap.basic.base.CommonResponse;
import com.mingqijia.gassafety.db.entity.FieldTemplate;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.constant.OperateRecordEnum;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import com.mingqijia.gassafety.webserver.response.BusinessNoticeResponse;
import com.mingqijia.gassafety.webserver.response.FuncAreaZoomResponse;
import com.mingqijia.gassafety.webserver.response.TemplateTypeResponse;
import com.mingqijia.gassafety.webserver.service.BusinessNoticeService;
import com.mingqijia.gassafety.webserver.service.DictionaryService;
import com.mingqijia.gassafety.webserver.service.FieldTemplateService;
import com.mingqijia.gassafety.shared.log.LogRecord;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Api(tags = "页面字段模板")
@RequestMapping("/business/v1/fieldPattern")
@Slf4j
public class FieldTemplateController {

   @Autowired
   FieldTemplateService fieldTemplateService;

   @Autowired
   BusinessNoticeService businessNoticeService;

   @Autowired
   DictionaryService dictionaryService;

   @GetMapping("/getTemplateType")
   @ApiOperation(value = "页面字段模板查询")
   public  CommonResponse<TemplateTypeResponse> getTemplateType(){
       FieldTemplate template = fieldTemplateService.getBySpId();
       PageUtil<BusinessNoticeResponse> list = businessNoticeService.getList(null, Constants.COCKPIT_TITLE, 1, 10);
       PageUtil<BusinessNoticeResponse> cockpitControlOrders = businessNoticeService.getList(null, Constants.COCKPIT_CONTROL_ORDER, 1, 10);
       PageUtil<BusinessNoticeResponse> infoList = businessNoticeService.getList(null, Constants.COCKPIT_WARNING_INFO, 1, 10);
       PageUtil<BusinessNoticeResponse> warnStrategyHide = businessNoticeService.getList(null, Constants.COCKPIT_WARN_STRATEGY_HIDE, 1, 10);

       TemplateTypeResponse response = new TemplateTypeResponse();
       response.setTemplateType(template.getTemplateType());
       response.setWorkSystemType(template.getWorkSystemType());
       response.setSkWorkSystemType(template.getSkWorkSystemType());
       response.setFuncAreaAllocat(template.getFuncAreaAllocat());
       response.setAlarmHandleType(template.getAlarmHandleType());
       response.setBrokenHandleType(template.getBrokenHandleType());
       if (CollectionUtil.isNotEmpty(list.getList())){
           response.setTitle(list.getList().get(0).getParamValue());
       }else {
           response.setTitle("");
       }
       String paramValue = "";
       if (CollectionUtil.isNotEmpty(cockpitControlOrders.getList())){
           paramValue = cockpitControlOrders.getList().get(0).getParamValue();
           response.setCockpitControlOrder(!StringUtils.isEmpty(paramValue) && "ON".equals(paramValue) ? 0 : 1);
       }
       if (CollectionUtil.isNotEmpty(infoList.getList())){
           paramValue = infoList.getList().get(0).getParamValue();
           response.setWarningInfoRoll( StringUtils.isEmpty(paramValue) || "ON".equals(paramValue) ? 1 : 0);
       }
       if (CollectionUtil.isNotEmpty(warnStrategyHide.getList())){
           paramValue = warnStrategyHide.getList().get(0).getParamValue();
           response.setWarnStrategyHide(!StringUtils.isEmpty(paramValue) && "ON".equals(paramValue) ? 0 : 1);
       }
       Map<String,String> funcAreaZoomMap = dictionaryService.getFuncAreaZoom(ApplicationUtils.getWorkingSpId());
       FuncAreaZoomResponse funcAreaZoomResponse = new FuncAreaZoomResponse();
       funcAreaZoomResponse.setCity(funcAreaZoomMap.getOrDefault("city",""));
       funcAreaZoomResponse.setDistrict(funcAreaZoomMap.getOrDefault("district",""));
       funcAreaZoomResponse.setStreet(funcAreaZoomMap.getOrDefault("street",""));
       response.setFuncAreaZoom(funcAreaZoomResponse);
       String mdp = funcAreaZoomMap.getOrDefault("mapDefaultZoom","0");
       response.setMapDefaultZoom(Integer.parseInt(mdp));
       String mbp = funcAreaZoomMap.getOrDefault("mapBreakPointZoom","0");
       response.setMapBreakPointZoom(Integer.parseInt(mbp));
       return CommonResponse.success(response);
   }

    /**
     * 设置字段模板
     * @return
     */
    @LogRecord(operateType = OperateRecordEnum.OPERATE_TYPE_21)
    @PostMapping("/setTemplateType")
    @ApiOperation(value = "页面字段模板设置")
    public CommonResponse<String> setTemplate(@RequestBody FieldTemplate template) {
        fieldTemplateService.setTemplateType(template);
        return CommonResponse.success("");
    }
}
